$("#shoupt").click(function (){
    var uid=$("#uid").val();
    var post_id=$("#post_id").val();
    var currentTop = $(this).attr("src");
    if (currentTop === "image/收藏pt2.png") {
        $(this).attr("src","image/收藏pt.png");

        $.ajax({
            url: "AddCollectionServlet",
            type: "post", // 显式指定请求类型为 POST
            data: {"uid": uid,"post_id":post_id},
            dataType: "text",
            success: function (rows) {
            }
        })


    } else {
        $(this).attr("src","image/收藏pt2.png");
        $.ajax({
            url: "DeleteCollectionServlet",
            type: "post", // 显式指定请求类型为 POST
            data: {"uid": uid,"post_id":post_id},
            dataType: "text",
            success: function (rows) {
            }
        })

    }
})
$("#dianpt").click(function () {
    var post_id=$("#post_id").val();
    // 获取当前元素的src属性值
    var currentSrc = $(this).attr("src");
    // 检查当前src是否为点赞状态
    if (currentSrc === "image/点赞pt.png") {
        // 获取点赞计数器的当前值
        var count = parseInt($("#dianptCount").text(), 10);
        // 更新计数器的值
        $("#dianptCount").text(count + 1);
        $("#dianzanCou").text(count+1)
        // 更改图片为已点赞状态
        $(this).attr("src", "image/点赞pt2.png");

        $.ajax({
            url: "AddLikeServlet",
            type: "post", // 显式指定请求类型为 POST
            data: {"post_id":post_id},
            success: function (rows) {
            }
        })

    } else {
        // 获取点赞计数器的当前值
        var count = parseInt($("#dianptCount").text(), 10);

        // 更新计数器的值
        // 确保计数器不会小于0
        if (count > 0) {
            $("#dianptCount").text(count - 1);
            $("#dianzanCou").text(count-1)
        }
        // 更改图片回未点赞状态
        $(this).attr("src", "image/点赞pt.png");


        $.ajax({
            url: "DeleteLikeServlet",
            type: "post", // 显式指定请求类型为 POST
            data: {"post_id":post_id},
            success: function (rows) {
            }
        })

    }
});




//发表评论
$("#sendPostCommentB").click(function (){
    // 获取所有 .reply-card 元素的数量
    var count = parseInt($("#cunnummment").text(), 10);
    // 对数字进行加一操作
    count += 1;
    // 更新 #cunnummment 元素的文本内容
    $("#cunnummment").text(count);
   //// 更新 #cunnummment 元素的文本内容
   //$("#cunnummment").text(count + 1);
    var currentDate = new Date();
    var year = currentDate.getFullYear();
    var month = currentDate.getMonth() + 1; // getMonth() 返回的是0-11
    var day = currentDate.getDate();
    var hours = currentDate.getHours();
    var minutes = currentDate.getMinutes();
    var seconds = currentDate.getSeconds();
// 格式化输出
    var ti=year+'-'+month+'-'+day+'  '+hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
    var message=$("#sendPostCommentV").text();
    var username=$("#nickname").val();
    var avatar=$("#avatar").val();
    var uid=$("#uid").val();
    var post_id=$("#post_id").val();
    var level=$("#level").val();
    //获取插入到节点
    var bo=$("#commentBDL");

    $.ajax({
        url: "AddCommentServlet",
        type: "post", // 显式指定请求类型为 POST
        data: {"post_id":post_id,"commentMessage": message},
        dataType: "text", // 可选，如果服务器返回的是纯文本可以保留
        success: function(cid) {
            var addcomment=" <div class=\"reply-card\" open-id=\"\"  >\n" +
                "                                            <div class=\"reply-card__left\"><a href=\"https://www.miyoushe.com/ys/accountCenter/postList?id=287744993\" class=\"mhy-router-link reply-card__link\" target=\"_blank\">\n" +
                "                                                <div class=\"mhy-avatar reply-card__avatar mhy-avatar__md\"><img src=\"image/"+avatar+"\" class=\"mhy-avatar__img\"> <!----> <!----></div>\n" +
                "                                            </a> <!----></div>\n" +
                "                                            <div class=\"reply-card__container\">\n" +
                "                                                <div class=\"reply-card__header\">\n" +
                "                                                    <div class=\"reply-card__account\"><a href=\"https://www.miyoushe.com/ys/accountCenter/postList?id=287744993\" class=\"mhy-router-link mhy-account-title reply-card__nickname\" target=\"_blank\"><span class=\"mhy-account-title__name\">"+username+"</span> <img src=\"image/level"+level+".png\" class=\"mhy-img-icon mhy-account-title__level\"> <!----> <!---->\n" +
                "                                                        <!----></a> <!----> <p class=\"reply-card__floor\"><!----> <span class=\"reply-card__region\">\n" +
                "\n" +
                "            </span></p></div>\n" +
                "                                                    <input type='button'   class=\"reply-card-operation-top deleteThisComment\" value='删除'>\n" +
                "                                                         <input type=\"hidden\" id=\"cid\" value=\""+cid+"\">\n" +
                "\n" +
                "                                                    </input>\n" +
                "                                                </div>\n" +
                "                                                <div class=\"reply-card__content\"><!---->\n" +
                "                                                    <pre><p>"+message+"</p></pre>\n" +
                "                                                </div>\n" +
                "                                                <div class=\"reply-card-operation-bottom\"><span class=\"reply-card__time\">"+ti+"<!----></span>\n" +
                "                                                    <div class=\"reply-card-operation-bottom__right\">\n" +
                "                                                        <div class=\"reply-card-operation-bottom__item rreply-card-operation-bottom__item--visible\">\n" +
                "                                                            回复\n" +
                "                                                        </div>\n" +
                "                                                        <div class=\"mhy-heart-click\">\n" +
                "                                                            <svg class=\"mhy-symbol-icon mhy-heart-click__icon\">\n" +
                "                                                                <use xlink:href=\"#icon-dianzanpc\"></use>\n" +
                "                                                            </svg>\n" +
                "                                                            <span>0</span></div>\n" +
                "                                                        <div class=\"mhy-down-click\">\n" +
                "                                                            <svg class=\"mhy-symbol-icon mhy-down-click__icon\">\n" +
                "                                                                <use xlink:href=\"#icon-diancaipc\"></use>\n" +
                "                                                            </svg>\n" +
                "                                                        </div>\n" +
                "                                                    </div>\n" +
                "                                                </div> <!----></div>\n" +
                "                                        </div>"
            bo.append(addcomment)

        },

    });
    location.reload()
})


var sp1= $('.mhy-follow-button').children('span').text()
$(document).ready(function(){
    var fid=$("#postuid").val()
    // 绑定点击事件到所有具有 mhy-follow-button 类的元素
    $('.mhy-follow-button').click(function() {
        var sp=$(this).children('span').text()
        if(sp==="关注" ){
            $(this).children().text("已关注")
            $(this).css("background-color","#ffffff")
            $(this).children().css("color","#596666")
            $.ajax({
                url: "AddConcernServlet",
                type: "post", // 显式指定请求类型为 POST
                data: {"fid":fid},
                success: function () {
                }
            })
        }else {
            $(this).children().text("关注")
            $(this).css("background-color","#00c3ff")
            $(this).children().css("color","#fff")
            $.ajax({
                url: "DeleteConcernServlet",
                type: "post", // 显式指定请求类型为 POST
                data: {"fid":fid},
                success: function () {
                }
            })

        }
    });
});

// Function to update image src paths
function updateImageSrc() {
    var img1 = document.getElementById('shoupt')
    var img2 = document.getElementById('dianpt')

    // Change the src attributes
    img1.src ='image/收藏pt.png';
    img2.src ='image/点赞pt2.png';
}

// Check if the page is being reloaded
window.onload = function() {
    if (performance.navigation.type === performance.navigation.TYPE_RELOAD) {
        updateImageSrc();
    }
};